package com.credithc.customers.base.mapper;

import com.credithc.baseserv.core.mapper.BsBaseMapper;
import com.credithc.customers.base.po.UnifySmsCodeFlowPo;
import com.credithc.customers.base.ro.SmsSendFlowRo;
import com.credithc.customers.base.vo.SmsSendFlowVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author fby
 * @date 2020/01/15 09:46
 */
@Mapper
@Repository
public interface UnifySmsMapper extends BsBaseMapper<UnifySmsCodeFlowPo> {

    @Select("SELECT sms_code AS smsCode FROM cust_unify_sms_flow WHERE phone = #{phone} and sub_system_sign = #{subSystemSign} and sms_code = #{smsCode} and create_time > DATE_SUB(NOW(), INTERVAL 10 MINUTE) " +
            " ORDER BY create_time DESC LIMIT 1")
    UnifySmsCodeFlowPo getUnifySmsCodeRecently(@Param("subSystemSign") String subSystemSign,@Param("phone") String phone,@Param("smsCode") String smsCode);

    @Select("SELECT id AS id, sms_code AS smsCode,create_time AS createTime FROM cust_unify_sms_flow WHERE phone = #{phone} and sub_system_sign = #{subSystemSign} and create_time > #{today} and create_time < #{tomorrow} and status = 1 " +
            " ORDER BY create_time DESC ")
    List<UnifySmsCodeFlowPo> getUnifySmsCodeToday(@Param("subSystemSign") String subSystemSign, @Param("phone") String phone, @Param("today") String today, @Param("tomorrow") String tomorrow);

    @Select({"<script>",
            "SELECT sms_code AS smsCode,create_time AS createTime FROM ",
            "cust_unify_sms_flow p",
            "WHERE create_time > DATE_SUB(NOW(), INTERVAL 10 MINUTE) and status = 1",
            "<when test='subSystemSign!=null'>",
            "AND p.sub_system_sign = #{subSystemSign}",
            "</when>",
            "<when test='phone!=null'>",
            "AND p.phone = #{phone}",
            "</when>",
            "<when test='ip!=null'>",
            "AND p.ip = #{ip}",
            "</when>",
            "</script>"})
    List<UnifySmsCodeFlowPo> getUnifySmsCodeFlowRecently(@Param("subSystemSign") String subSystemSign,@Param("phone") String phone,@Param("ip") String ip);

    @Select("SELECT sms_code AS smsCode,unlock_time AS unlockTime FROM cust_unify_sms_flow WHERE phone = #{phone} and sub_system_sign = #{subSystemSign} and status = 1 " +
            " ORDER BY create_time DESC LIMIT 1")
    UnifySmsCodeFlowPo getUnifySmsCode(@Param("subSystemSign") String subSystemSign,@Param("phone") String phone);

    int getListCount(@Param("ro") SmsSendFlowRo ro);

    List<SmsSendFlowVo> getList(@Param("ro") SmsSendFlowRo ro);
}
